﻿using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using XNA_Game_Engine;
using XNA_Game_Engine.Characters;
using XNA_Game_Engine.Map;
using XNA_Game_Engine.GUI;
using XNA_Game_Engine.Sounds;

namespace MenesDroid.EngineCharacter
{
    class Robot : Character
    {
        private IsometricTilemap map;
        public int XDir, YDir;
        private float fxTimer = 0;
        private float timer = 0;
        private Boolean jumping = false;
        public bool isAbovePlataform = false;
        public int playerXDirInicial;
        public int playerYDirInicial;
        public bool canJump = false;
        private int playerXInicial;
        private int playerYInicial;
        private AnimatedSprite Sprite;
        private bool isDead = false;
        private Texture2D enemyKillSprite;
        private float scale;

        public Robot( int x, int y, AnimatedSprite sprite, IsometricTilemap tileMap, Texture2D enmyKill )
        {
            this.x = x;
			this.y = y;
            this.Sprite = sprite;
            this.map = tileMap;
            Angle = 27;
            Speed = 100;
            Width = sprite.GetWidth();
            Height = sprite.GetHeight();
            this.XDir = 0;
            this.YDir = 1;
            this.enemyKillSprite = enmyKill;
            //playerXInicial = x;
            //playerYInicial = y;   
        }


        public override void Update(float gameTime)
        {
            //OldX = x; OldY = y;
            Sprite.Update(gameTime);

            //double velx = Math.Cos( MathHelper.ToRadians(Angle)) * Speed;
            //double vely = Math.Sin( MathHelper.ToRadians(Angle)) * Speed;
           // x += velx * gameTime / 1000;
            //y += vely * gameTime / 1000;
            
            setAnimFrame();
            KeyboardInputEvents(gameTime);
            effectsControl();
        }


        public override void Render( SpriteBatch renderTarget )
        {
            int borderDistance = 5;
            float posY = (float) y ;
            float posX = (float) x;
            
            float tx = (float) ((posX * map.TileWidth / 2) + (posY * map.TileWidth / 2) + map.X) + 9 + borderDistance;
            float ty = (float)((posY * (map.TileHeigth - 9) / 2) - (posX * (map.TileHeigth - 9) / 2) + map.Y) - Height/2;

            if(jumping)
            {
                tx -= 2;
                ty -= 10;
            }

            if(!isDead)
                Sprite.Render(renderTarget, tx, ty);
            else                   
              renderDeathEffect(renderTarget, tx, ty);
          
        }

        private void renderDeathEffect(SpriteBatch renderTarget, float tx, float ty)
        {
            int size = enemyKillSprite.Width;
            Rectangle rect = new Rectangle(0, 0, enemyKillSprite.Width, enemyKillSprite.Height);

            renderTarget.Draw(enemyKillSprite,
                new Vector2(tx, ty),
                rect,
                Color.White,
                0,
                new Vector2(size / 2, size / 2),
                scale,
                SpriteEffects.None,
                0);
        }

        private void effectsControl()
        {
            if (isDead)
            {
                fxTimer++;
                if (fxTimer <= 20) // Effects Control
                {
                    if (scale <= 0.5)
                        scale = scale + 0.1f;
                }
                else
                {                   
                    StaticGui.TimeStart.Stop(); // Para o jogo apos o efeito ter terminado;
                    isDead = false;                    
                    scale = 0;
                    fxTimer = 0;
                   
                }
            }
        }

        public void setPlrSpawnDirectionFrame(Vector2 checkPointPos)
        {

            if (this.X < checkPointPos.X)
            {
                if (this.X - checkPointPos.X > -2)
                {
                    this.XDir = 1; this.YDir = 0; //Console.WriteLine("1");// DIREITA 

                }
                else
                {
                    if (this.Y - checkPointPos.Y > -2 && this.Y - checkPointPos.Y < 0)
                    {
                        this.XDir = 0; this.YDir = 1; //Console.WriteLine("2");//BAIXO
                    }
                    else
                    {
                        if (this.Y - checkPointPos.Y < 0)
                        {
                            this.XDir = 1; this.YDir = 0; //Console.WriteLine("3");  //DIREITA
                        }
                        if (this.Y - checkPointPos.Y > 2)
                        {
                            this.XDir = 0; this.YDir = -1; //Console.WriteLine("4"); //CIMA
                        }
                        else
                        {
                            if (this.Y - checkPointPos.Y > 0)
                            {
                                this.XDir = 1; this.YDir = 0; //Console.WriteLine("5");//DIREITA
                            }
                        }
                    }
                }
            }
            else
            {
                if (this.X - checkPointPos.X > 2)
                {
                    this.XDir = -1; this.YDir = 0; //Console.WriteLine("6");// ESQUERDA
                }
                else
                {
                    if (this.Y - checkPointPos.Y > -2 && this.Y - checkPointPos.Y < 0)
                    {
                        this.XDir = 0; this.YDir = 1; //Console.WriteLine("7"); //BAIXO
                    }
                    else
                    {
                        if (this.Y - checkPointPos.Y < 0)
                        {
                            this.XDir = -1; this.YDir = 0; //Console.WriteLine("8");//ESQUERDA
                        }
                        if (this.Y - checkPointPos.Y > 2)
                        {
                            this.XDir = 0; this.YDir = -1; //Console.WriteLine("9");//CIMA
                        }
                        else
                        {
                            if (this.Y - checkPointPos.Y > 0)
                            {
                                this.XDir = -1; this.YDir = 0; //Console.WriteLine("10");//ESQUERDA
                            }
                        }
                    }
                }
            }



            if (checkPointPos.X == this.X)
            {
                if (checkPointPos.Y > this.Y)
                {
                    this.XDir = 0; this.YDir = 1; //Console.WriteLine("11"); //BAIXO                    
                }
                else
                {
                    this.XDir = 0; this.YDir = -1; //Console.WriteLine("12"); //CIMA
                }
            }

            if (checkPointPos.Y == this.Y)
            {
                if (checkPointPos.X > this.X)
                {
                    this.XDir = 1; this.YDir = 0; //Console.WriteLine("13");//DIREITA                  
                }
                else
                {
                    this.XDir = -1; this.YDir = 0;// Console.WriteLine("14");//ESQUERDA
                }
            }


            playerXDirInicial = XDir;
            playerYDirInicial = YDir;

            setAnimFrame();

          

        }
        
        public void moveAhead()
        {
            OldX = x; OldY = y;

            if ((isJumping() && isAbovePlataform) || (!isJumping() && !isAbovePlataform))
            {
                this.x += XDir;
                this.y += YDir;
            }
            mapBounds();
        }

        public void moveBack()
        {
            
            this.x += -XDir;
            this.y += -YDir;                 
            mapBounds();
        }

        public void jumpAhead()
        {
            if (jumping == false)
            {
                if (this.canJump == true)
                {
                    jumping = true;

                    this.x += XDir;
                    this.y += YDir;
                }
            }
            else
                jumpOff();

        }

        public void jumpOff()
        {
            jumping = false;
            moveAhead();
        }

        public void jumpFalse()
        {
            jumping = false;
        }

        public void turnLeft()
        {
            float xAng = XDir;
            float yAng = YDir;
            float angulo = MathHelper.ToRadians(90);
            float coseno = (float)Math.Cos(angulo);
            float seno   = (float)Math.Sin(angulo);

            XDir = (int)(xAng * coseno + yAng * seno);
            YDir = (int)(-xAng * seno + yAng * coseno);
        }

        public void turnRight()
        {
            float xAng = XDir;
            float yAng = YDir;
            float angulo = MathHelper.ToRadians(90);
            float coseno = (float)Math.Cos(angulo);
            float seno   = (float)Math.Sin(angulo);

            XDir = (int)(xAng * coseno - yAng * seno);
            YDir = (int)(xAng * seno + yAng * coseno);
        }

        private void mapBounds()
        {
            this.x = this.x >= 9 ? 9 : this.x;
            this.y = this.y >= 9 ? 9 : this.y;
            this.x = this.x < 0 ? 0 : this.x;
            this.y = this.y < 0 ? 0 : this.y;
        }

        private void setAnimFrame()
        {
            if (XDir == 0 && YDir == 1)
                Sprite.SetFrameLine(2, 1); //BAIXO

            if (XDir == 1 && YDir == 0)
                Sprite.SetFrameLine(3, 1); //DIREITA

            if (XDir == 0 && YDir == -1)
                Sprite.SetFrameLine(1, 1); //CIMA

            if (XDir == -1 && YDir == 0)
                Sprite.SetFrameLine(0, 1); //ESQUERDA

        }

        public void restartOriginalPositions()
        {
            StaticGui.PlayerReference.X = playerXInicial;
            StaticGui.PlayerReference.Y = playerYInicial;
            this.XDir = this.playerXDirInicial;
            this.YDir = this.playerYDirInicial;
        }
      

        public int oldX
        {
            get { return (int)OldX; }
        }

        public int oldY
        {
            get { return (int)OldY; }
        }


        public bool isJumping()
        {
            return jumping;
        }

        public int xDir
        {
            get { return XDir; } 
            set { XDir = value; } 
        }

        public int XInicial
        {
            get { return playerXInicial; } 
            set { playerXInicial = value; }
        }

        public int YInicial
        {
            get { return playerYInicial; } 
            set { playerYInicial = value; }
        }

        public int yDir
        {
            get { return YDir; }
            set { XDir = value; }
        }

        public bool PlrDead
        {
            get { return isDead; }
            set { isDead = value; }
        }

        private void KeyboardInputEvents(float gameTime)
        {
            Speed = 0;
            timer += gameTime;

            if (timer >= 100)
            {
                if (Keyboard.GetState().IsKeyDown(Keys.W))
                {
                    moveAhead();
                }
                if (Keyboard.GetState().IsKeyDown(Keys.S))
                {
                    moveBack();
                }
                if (Keyboard.GetState().IsKeyDown(Keys.A))
                {
                    turnLeft();
                }

                if (Keyboard.GetState().IsKeyDown(Keys.D))
                {
                    turnRight();
                }

                if (Keyboard.GetState().IsKeyDown(Keys.Space))
                {
                    jumpAhead();
                }


                timer = 0;
            }
        }


       

    } // end of PlayerCharacter class
} //  MenesDroid.Character namespace
